package main;

import static util.Constants.DEFAULT_CAMERA_MOVING_STEP;
import static util.Constants.DEFAULT_MOUSE_SENSITIVITY_FACTOR;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import com.sun.corba.se.impl.oa.poa.ActiveObjectMap.Key;

public class JOGLEventListener implements KeyListener, MouseListener, 
       MouseMotionListener, ActionListener
{
    /**
     * A reference to the attached application.
     */
	private MotionSimulator _ga;

    /**
     * Position of most recent mouse click.
     */
	private Point _lastMousePos;

    /**
     * Determines if the left mouse button is pressed.
     */
	private boolean _isLeftButtonDown;

    /**
     * Determines how much the camera moves each step.
     */
    private double _cameraMovingStep;

    /**
     * Determines a mouse sensitivity factor.
     */
    private double _mouseSensitivityFactor;
	
    /**
     * Create a new default event listener.
     *
     * @param MotionSimulator a reference to the application
     */
	public JOGLEventListener(MotionSimulator ga_)
    {
        _ga = ga_;
		_isLeftButtonDown = false;
        _cameraMovingStep = DEFAULT_CAMERA_MOVING_STEP;
        _mouseSensitivityFactor= DEFAULT_MOUSE_SENSITIVITY_FACTOR;
	}
	
    @Override
	public void keyReleased(KeyEvent e_)
    {
    }

    @Override
	public void keyPressed(KeyEvent e_)
    {
        assert _ga != null;

		int iKey = e_.getKeyCode();
		switch (iKey)
        {
		case KeyEvent.VK_UP:
            _ga.getCamera().freeMove(0, 0, _cameraMovingStep);
            break;
        case KeyEvent.VK_DOWN:
            _ga.getCamera().freeMove(0, 0, -_cameraMovingStep);
            break;
		case KeyEvent.VK_LEFT:	
            _ga.getCamera().freeMove(_cameraMovingStep, 0, 0);
            break;
		case KeyEvent.VK_RIGHT:
            _ga.getCamera().freeMove(-_cameraMovingStep, 0, 0);
            break;
		case KeyEvent.VK_U:
            _ga.getCamera().freeMove(0, _cameraMovingStep, 0);
            break;
		case KeyEvent.VK_D:
            _ga.getCamera().freeMove(0, -_cameraMovingStep, 0);
            break;
		case KeyEvent.VK_S:
			_ga.toggleStepping();
			break;
		case KeyEvent.VK_B:
			_ga.toggleRenderBoundingBoxMode();
		case KeyEvent.VK_F:
			_ga.stepping();
			break;
		case KeyEvent.VK_R:
            _ga.toggleFrameExporter();
            break;
		}
	}

    @Override
	public void keyTyped(KeyEvent e_)
    {
    }

    @Override
	public void mouseMoved(MouseEvent e_)
    {
    }

    @Override
	public void mouseDragged(MouseEvent e_)
    {
		if (_isLeftButtonDown)
        {
			Point newMousePos = e_.getPoint();

			int dx = newMousePos.x - _lastMousePos.x;
			int dy = newMousePos.y - _lastMousePos.y;

			if (dx != 0)
            {
                _ga.getCamera().rotateY(-dx * _mouseSensitivityFactor);
            }
            if (dy != 0)
            {
                _ga.getCamera().rotateX(dy * _mouseSensitivityFactor);
            }

			_lastMousePos = newMousePos;
		}
	}

    @Override
	public void mousePressed(MouseEvent e_)
    {
		if (e_.getButton() == MouseEvent.BUTTON1) 
        {
			_isLeftButtonDown = true;
			_lastMousePos = e_.getPoint();
		}
	}

    @Override
	public void mouseReleased(MouseEvent e_)
    {
		if (e_.getButton() == MouseEvent.BUTTON1)
        {
			_isLeftButtonDown = false;
		}
	}

    @Override
	public void mouseClicked(MouseEvent e_)
    {
    }

    @Override
	public void mouseEntered(MouseEvent e_)
    {
    }

    @Override
	public void mouseExited(MouseEvent e_)
    {
    }
	
    @Override
	public void actionPerformed(ActionEvent e_)
    {
		String s = e_.getActionCommand();
		if (s.equals("Exit"))
        {
            System.exit(0);
        }
	}
}
